package com.jquest.data;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.Color;

import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: Johan
 * Date: 2009-dec-11
 * Time: 03:26:39
 * To change this template use File | Settings | File Templates.
 */
public class Character extends AbstractEntity {

    private HashMap<String, Animation> animations;

    private String currentAnimation = "WALK_DOWN";

    private boolean isMoving = false;

    public Character(float x, float y, SpriteSheet ss, int duration) {
        super(x, y);
        animations = new HashMap<String, Animation>();
        animations.put("WALK_LEFT", new Animation(new SpriteSheet(ss.getSubImage(0, 0, 135, 24), 16, 24, 1), duration));
        animations.put("WALK_RIGHT", new Animation(new SpriteSheet(ss.getSubImage(0, 25, 135, 24), 16, 24, 1), duration));
        animations.put("WALK_DOWN", new Animation(new SpriteSheet(ss.getSubImage(0, 50, 135, 24), 16, 24, 1), duration));
        animations.put("WALK_UP", new Animation(new SpriteSheet(ss.getSubImage(0, 75, 135, 24), 16, 24, 1), duration));
    }

    public boolean isMoving() {
        return isMoving;
    }

    public void setMoving(boolean moving) {
        this.isMoving = moving;
    }

    public Animation getAnimation() {
        return animations.get(currentAnimation);
    }

    public void setAnimation(String key) {
        this.currentAnimation = key;
    }

    public void stopMove() {
        animations.get(currentAnimation).setCurrentFrame(0);
        animations.get(currentAnimation).stop();
    }

    public void update(int time) {
        if(getCollisionMask() != null) {
            getCollisionMask().update(time);
        }
        if(isMoving()) {
            animations.get(currentAnimation).update(time);
        }
    }

    public void render(Graphics g, Object attachment) {
        g.drawAnimation(animations.get(currentAnimation), getX(), getY());
        if(getCollisionMask() != null) {
            getCollisionMask().render(g, attachment);
        }
    }
    
}
